#include "../../include/ShowInfo.h"

using namespace std;

StudentController* showStuController;
CarController* showCarController;
CompareController* showCompareController;

void showInfo(CompareController* _compareController,StudentController* _stuController,CarController* _carController) {
    showStuController = _stuController;
    showCarController = _carController;
    showCompareController = _compareController;
    int idx = 0;
    int carListLength = showCarController->getCarCount();
    
    if(carListLength <= 0) {
        cout << "当前没有任何车辆信息!" << endl;
        return;
    }

    SmartCar* carList[carListLength];
    showCarController->listAllCar(carList);

    string cmd;     // 输入命令
    while (cmd != E_X_I_T ) {

        cout << "=========车辆信息展示界面==========" << endl;
        cout << "当前展示小车序号为: " << idx+1  << " ,共有 " << carListLength << " 辆小车" << endl;
        printCar(carList[idx]);
        cout << "指令说明:" << endl;
        cout << "n: 下一辆      |       p: 上一辆" << endl;
        cin >> cmd;
        if(cmd == "n") {
            if(idx < carListLength-1) idx += 1;
        }
        else if(cmd == "p") {
            if(idx > 0) idx -= 1;
        }
    }
}
void printCar(SmartCar* car) {
    int compareCount = showCompareController->getCompareCountByCarId(car->getId());
    Compare* compareList[compareCount];
    showCompareController->findCompareByCarId(car->getId(),compareList);
    int componentCount = car->getComponentCount();
    Component* componentList[componentCount];
    car->listAllComponent(componentList);
    cout << endl;
    cout << "<车辆信息>" << endl;
    cout << "编号: " << car->getId() << endl;
    printBlank(1);
    cout << "匹配状态: ";
    if(compareCount == 0) {
        cout << "未分配" << endl;
    }
    else {
        cout << "已分配给" << compareCount << "位同学" << endl;
        printBlank(1);
        cout << "分配学生列表: ";
        for(int i=0;i<compareCount;i++) {
            string stuId = compareList[i]->getStuId();
            Student* student = showStuController->findStudentById(stuId);
            cout << student->getName() << " ";
        }
        cout << endl;
    }
    for(int i=0;i<componentCount;i++) {
        printComponent(componentList[i],1);
    }
    cout << endl;
}
void printComponent(Component* component,int level) {
    int paramCount = component->getParamCount();
    Param* paramList[paramCount];
    component->listAllParam(paramList);
    int subComponentCount = component->getSubComponentCount();
    Component* subComponentList[subComponentCount];
    component->listAllSubComponent(subComponentList);
    printBlank(level);
    cout << component->getName() << endl;
    for(int i=0;i<paramCount;i++) {
        printParam(paramList[i],level+1);
    }
    for(int i=0;i<subComponentCount;i++) {
        printComponent(subComponentList[i],level+1);
    }
}

void printParam(Param* param,int level) {
    printBlank(level);
    cout << param->getName() << ": " << param->getValue();
    if(param->getUnit() != "none") {
        cout << " " << param->getUnit() << "  ";
    }
    if(param->getRemark() != "none") {
        cout << param -> getRemark();
    }
    cout << endl;
}

inline void printBlank(int level) {
    for(int i=0;i<level;i++) {
        cout << "  ";
    }
}